<!DOCTYPE html>
<html lang="cn" class="app fadeInUp animated">
<head>
  <meta charset="utf-8"/>
  <title>{$_SERVER['WEB_NAME']}—国内最专业的小程序管理平台</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
  <link href="/assets/stylesheets/bootstrap.min.css" rel="stylesheet"/>
  <link href="/assets/stylesheets/font-awesome.min.css" rel="stylesheet"/>
</head>

<body>

<div class="container-fluid" style="padding-top: 15px">
  <div class="row">
    <div class="col-md-12">
      <!--短信购买-->
      <div class="panel panel-default">
        <div class="panel-heading">
          短信购买
        </div>
        <div class="panel-body">
          <table class="table table-bordered table-striped">
            <tr>
              <td width="100">短信价格</td>
              <td>{$agency->sms_price}</td>
            </tr>
            <tr>
              <td>账户余额</td>
              <td>￥{sprintf('%.2f', $user->balance)}</td>
            </tr>
            <tr>
              <td style="vertical-align: middle">购买条数</td>
              <td>
                <input type="number" name="buy_num" id="buy_num" class="form-control" style="width: 100px" min="1" step="1" value="100">
              </td>
            </tr>
            <tr>
              <td>价格合计</td>
              <td><span id="show_total_price"></span>元</td>
            </tr>
          </table>
          <div>
            <a href="javascript:void(0)" class="btn btn-success" id="btn-buy">
              购买
            </a>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>
<script src="/mix/jquery.min.js"></script>
<script src="/mix/layer.js"></script>
<script>
  $('#btn-buy').on('click', function () {
    if(isNaN(document.getElementById('buy_num').value) || document.getElementById('buy_num').value==''){
      alert('请输入正确的购买数量');
      return false;
    }
    var buy_num = parseInt(document.getElementById('buy_num').value);
    $.ajax({
      url: './buy.html',
      method: 'post',
      data: {
        'num': buy_num
      },
      success: function (resp) {
        alert(resp.msg);
        // location.reload(1);
        location.href='balance.html'
      },
      error: function () {
        layer.msg('出现网络错误，请稍后重试');
      }
    });
  });
  var compute_price = function () {
    var one_price = Number("{$agency->sms_price}");
    var buy_num = parseInt(document.getElementById('buy_num').value);
    document.getElementById('buy_num').value = buy_num;
    document.getElementById('show_total_price').innerText = one_price * 100 * buy_num / 100;
  };
  $().ready(function () {
    compute_price();
  });
  document.getElementById('buy_num').oninput = function () {
    compute_price();
  };
</script>

</body>

</html>
